package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by gamy on 2017/7/15.
 */
public class ReadLineUtil {

    public static abstract class ReadLineHandler{
        public abstract void begin();
        public abstract void handle(String lineStr);
        public abstract void end();
    }

    public static void readLine(String fileName, ReadLineHandler handler){
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            handler.begin();
            while ((tempString = reader.readLine()) != null) {
                handler.handle(tempString);
            }
            handler.end();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}
